library(dplyr)
library(tidyverse)
library(haven)
library(foreign)
library(stargazer)
library(lfe)

load(file="congress_analysis.RData")


### Table 1
## uncontestedness, measures of partisan bias based on congressional elections
####
state_elections_congress$treatment<-scale(state_elections_congress$EG.L1)
reg1<-(felm(dem_unc_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$declination2.L1)
reg2<-(felm(dem_unc_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median.L1)
reg3<-(felm(dem_unc_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry.L1)
reg4<-(felm(dem_unc_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$factor.L1)
reg5<-(felm(dem_unc_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
#stargazer(reg1, reg2, reg3, reg4, reg5)
state_elections_congress$treatment<-scale(state_elections_congress$EG.L1)
reg1.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$declination2.L1)
reg2.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median.L1)
reg3.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry.L1)
reg4.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$factor.L1)
reg5.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))

### Table A1
## uncontestedness, measures of partisan bias based on presidential elections
####
state_elections_congress$treatment<-scale(state_elections_congress$EG_pres)
reg1<-(felm(dem_unc_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$declination2_pres)
reg2<-(felm(dem_unc_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median_pres)
reg3<-(felm(dem_unc_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry_pres)
reg4<-(felm(dem_unc_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$factor_pres)
reg5<-(felm(dem_unc_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
#stargazer(reg1, reg2, reg3, reg4, reg5)
state_elections_congress$treatment<-scale(state_elections_congress$EG_pres)
reg1.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$declination2_pres)
reg2.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median_pres)
reg3.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry_pres)
reg4.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$factor_pres)
reg5.1<-(felm(dem_unc_adv~treatment+dem_unc_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))





### Table 2
## candidate quality, measures of partisan bias based on congressional elections
####
state_elections_congress$treatment<-scale(state_elections_congress$EG.L1)
reg1<-(felm(qual_D_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$declination2.L1)
reg2<-(felm(qual_D_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median.L1)
reg3<-(felm(qual_D_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry.L1)
reg4<-(felm(qual_D_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$factor.L1)
reg5<-(felm(qual_D_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
#stargazer(reg1, reg2, reg3, reg4, reg5)
state_elections_congress$treatment<-scale(state_elections_congress$EG.L1)
reg1.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$declination2.L1)
reg2.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median.L1)
reg3.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry.L1)
reg4.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$factor.L1)
reg5.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))

### Table A2
## candidate quality, measures of partisan bias based on presidential elections
state_elections_congress$treatment<-scale(state_elections_congress$EG_pres)
reg1<-(felm(qual_D_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$declination2_pres)
reg2<-(felm(qual_D_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median_pres)
reg3<-(felm(qual_D_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry_pres)
reg4<-(felm(qual_D_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$factor_pres)
reg5<-(felm(qual_D_adv~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
#stargazer(reg1, reg2, reg3, reg4, reg5)
state_elections_congress$treatment<-scale(state_elections_congress$EG_pres)
reg1.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$declination2_pres)
reg2.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median_pres)
reg3.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry_pres)
reg4.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$factor_pres)
reg5.1<-(felm(qual_D_adv~treatment+qual_D_adv.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))

### Table 3
## fundraising, measures of partisan bias based on congressional elections
####
state_elections_congress$treatment<-scale(state_elections_congress$EG.L1)
reg1<-(felm(total_receipts_perc_dem~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$declination2.L1)
reg2<-(felm(total_receipts_perc_dem~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median.L1)
reg3<-(felm(total_receipts_perc_dem~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry.L1)
reg4<-(felm(total_receipts_perc_dem~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$factor.L1)
reg5<-(felm(total_receipts_perc_dem~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
#stargazer(reg1, reg2, reg3, reg4, reg5)
state_elections_congress$treatment<-scale(state_elections_congress$EG.L1)
reg1.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$declination2.L1)
reg2.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median.L1)
reg3.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry.L1)
reg4.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$factor.L1)
reg5.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))

### Table A3
## fundraising, measures of partisan bias based on presidential elections
state_elections_congress$treatment<-scale(state_elections_congress$EG_pres)
reg1<-(felm(total_receipts_perc_dem~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$declination2_pres)
reg2<-(felm(total_receipts_perc_dem~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median_pres)
reg3<-(felm(total_receipts_perc_dem~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry_pres)
reg4<-(felm(total_receipts_perc_dem~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$factor_pres)
reg5<-(felm(total_receipts_perc_dem~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
#stargazer(reg1, reg2, reg3, reg4, reg5)
state_elections_congress$treatment<-scale(state_elections_congress$EG_pres)
reg1.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$declination2_pres)
reg2.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median_pres)
reg3.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry_pres)
reg4.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$factor_pres)
reg5.1<-(felm(total_receipts_perc_dem~treatment+total_receipts_perc_dem.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))


### Table 4
## mean_voteshare, measures of partisan bias based on congressional elections
####
state_elections_congress$treatment<-scale(state_elections_congress$EG.L1)
reg1<-(felm(mean_voteshare~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$declination2.L1)
reg2<-(felm(mean_voteshare~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median.L1)
reg3<-(felm(mean_voteshare~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry.L1)
reg4<-(felm(mean_voteshare~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$factor.L1)
reg5<-(felm(mean_voteshare~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
#stargazer(reg1, reg2, reg3, reg4, reg5)
state_elections_congress$treatment<-scale(state_elections_congress$EG.L1)
reg1.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$declination2.L1)
reg2.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median.L1)
reg3.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry.L1)
reg4.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
state_elections_congress$treatment<-scale(state_elections_congress$factor.L1)
reg5.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|STATEAB+election|0|STATEAB, data=state_elections_congress[state_elections_congress$first_year==0,]))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))

### Table A4
## mean_voteshare, measures of partisan bias based on presidential elections
state_elections_congress$treatment<-scale(state_elections_congress$EG_pres)
reg1<-(felm(mean_voteshare~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$declination2_pres)
reg2<-(felm(mean_voteshare~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median_pres)
reg3<-(felm(mean_voteshare~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry_pres)
reg4<-(felm(mean_voteshare~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$factor_pres)
reg5<-(felm(mean_voteshare~treatment|STATEAB+election|0|STATEAB, data=state_elections_congress))
#stargazer(reg1, reg2, reg3, reg4, reg5)
state_elections_congress$treatment<-scale(state_elections_congress$EG_pres)
reg1.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$declination2_pres)
reg2.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$mean_median_pres)
reg3.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$symmetry_pres)
reg4.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
state_elections_congress$treatment<-scale(state_elections_congress$factor_pres)
reg5.1<-(felm(mean_voteshare~treatment+mean_voteshare.L1|STATEAB+election|0|STATEAB, data=state_elections_congress))
stargazer(reg1,reg1.1, reg2, reg2.1,reg3,reg3.1, reg4,reg4.1, reg5, reg5.1, star.cutoffs = c(0.1, 0.05))

####
## The following code creates the plots for Figure 1
#####


## graph for fundraising
state_elections_congress$random<-rnorm(dim(state_elections_congress)[1])
width=.1
reg2.graph.outcome2<-(felm(total_receipts_perc_dem~random|STATEAB+election, data=state_elections_congress[!is.na(state_elections_congress$factor.L1),]))
reg2.graph.treatment2<-(felm(scale(factor.L1)~random|STATEAB+election, data=state_elections_congress[!is.na(state_elections_congress$total_receipts_perc_dem),]))
predictions.outcome2<-resid(reg2.graph.outcome2)
predictions.treatment2<-resid(reg2.graph.treatment2)
data.predictions2<-state_elections_congress[  !is.na(state_elections_congress$total_receipts_perc_dem)& !is.na(state_elections_congress$factor.L1),]
data.predictions2<-select(data.predictions2, STATEAB, election)
colnames( predictions.outcome2)<-"predictions.outcome"
colnames( predictions.treatment2)<-"predictions.treatment"
data.predictions2<-cbind( predictions.outcome2, predictions.treatment2)

data.predictions2<-as.data.frame(data.predictions2)
graph <- mutate(data.predictions2, bin=cut(predictions.treatment, breaks=seq(-2,2, width)))
mean_outcome<-summarise(graph, mean(predictions.outcome))
mean_outcome_raw<-summarise(graph, mean(predictions.outcome))

graph$predictions.outcome<-graph$predictions.outcome-mean(graph$predictions.outcome)
bin1.df <- data.frame(

bin.mean=tapply(graph$predictions.outcome,
                         graph$bin, mean, na.rm=TRUE),
                     mid=seq(-2+width/2 , 2-width/2 , width),
                     N=tapply(graph$predictions.outcome, graph$bin, length))
bin1.df$N_adjusted<-bin1.df$N*bin1.df$sample
library(ggplot2)
c <- ggplot(data=data.predictions2, aes(predictions.treatment, predictions.outcome))
c <- c + geom_point(data=bin1.df,aes(mid, bin.mean,size=N))
c <- c + stat_smooth(method = "lm")
c <- c + ylab("Residualized Democratic Share of Fundraising")
c <- c + xlab("Residualized, Lagged Democratic
 Advantage in Districting Process") 
 c <- c + theme_bw() +theme(axis.title=element_text(size=14),axis.text=element_text(size=14))
#c
pdf(file="Figures/congress_total_receipts_perc_dem.pdf")
c
dev.off()
ggsave(file="Figures/congress_total_receipts_perc_dem.eps")
c
dev.off()


## graph for contestedness
state_elections_congress$random<-rnorm(dim(state_elections_congress)[1])
width=.1
reg2.graph.outcome2<-(felm(dem_unc_adv~random|STATEAB+election, data=state_elections_congress[!is.na(state_elections_congress$factor.L1),]))
reg2.graph.treatment2<-(felm(scale(factor.L1)~random|STATEAB+election, data=state_elections_congress[!is.na(state_elections_congress$dem_unc_adv),]))
predictions.outcome2<-resid(reg2.graph.outcome2)
predictions.treatment2<-resid(reg2.graph.treatment2)
data.predictions2<-state_elections_congress[  !is.na(state_elections_congress$dem_unc_adv)& !is.na(state_elections_congress$factor.L1),]
data.predictions2<-select(data.predictions2, STATEAB, election)
colnames( predictions.outcome2)<-"predictions.outcome"
colnames( predictions.treatment2)<-"predictions.treatment"
data.predictions2<-cbind( predictions.outcome2, predictions.treatment2)

data.predictions2<-as.data.frame(data.predictions2)
graph <- mutate(data.predictions2, bin=cut(predictions.treatment, breaks=seq(-2,2, width)))
mean_outcome<-summarise(graph, mean(predictions.outcome))
mean_outcome_raw<-summarise(graph, mean(predictions.outcome))

graph$predictions.outcome<-graph$predictions.outcome-mean(graph$predictions.outcome)
bin1.df <- data.frame(

bin.mean=tapply(graph$predictions.outcome,
                         graph$bin, mean, na.rm=TRUE),
                     mid=seq(-2+width/2 , 2-width/2 , width),
                     N=tapply(graph$predictions.outcome, graph$bin, length))
bin1.df$N_adjusted<-bin1.df$N*bin1.df$sample
library(ggplot2)
c <- ggplot(data=data.predictions2, aes(predictions.treatment, predictions.outcome))
c <- c + geom_point(data=bin1.df,aes(mid, bin.mean,size=N))
c <- c + stat_smooth(method = "lm")
c <- c + ylab("Residualized Democratic Share of Uncontested Seats")
c <- c + xlab("Residualized, Lagged Democratic
 Advantage in Districting Process")
 c <- c + theme_bw() +theme(axis.title=element_text(size=14),axis.text=element_text(size=14))
#c
pdf(file="Figures/congress_dem_unc_adv.pdf")
c
dev.off()

ggsave(file="Figures/congress_dem_unc_adv.eps")
c
dev.off()

## graph for quality
state_elections_congress$random<-rnorm(dim(state_elections_congress)[1])
width=.1
reg2.graph.outcome2<-(felm(qual_D_adv~random|STATEAB+election, data=state_elections_congress[!is.na(state_elections_congress$factor.L1),]))
reg2.graph.treatment2<-(felm(scale(factor.L1)~random|STATEAB+election, data=state_elections_congress[!is.na(state_elections_congress$qual_D_adv),]))
predictions.outcome2<-resid(reg2.graph.outcome2)
predictions.treatment2<-resid(reg2.graph.treatment2)
data.predictions2<-state_elections_congress[  !is.na(state_elections_congress$qual_D_adv)& !is.na(state_elections_congress$factor.L1),]
data.predictions2<-select(data.predictions2, STATEAB, election)
colnames( predictions.outcome2)<-"predictions.outcome"
colnames( predictions.treatment2)<-"predictions.treatment"
data.predictions2<-cbind( predictions.outcome2, predictions.treatment2)

data.predictions2<-as.data.frame(data.predictions2)
graph <- mutate(data.predictions2, bin=cut(predictions.treatment, breaks=seq(-2,2, width)))
mean_outcome<-summarise(graph, mean(predictions.outcome))
mean_outcome_raw<-summarise(graph, mean(predictions.outcome))

graph$predictions.outcome<-graph$predictions.outcome-mean(graph$predictions.outcome)
bin1.df <- data.frame(

bin.mean=tapply(graph$predictions.outcome,
                         graph$bin, mean, na.rm=TRUE),
                     mid=seq(-2+width/2 , 2-width/2 , width),
                     N=tapply(graph$predictions.outcome, graph$bin, length))
bin1.df$N_adjusted<-bin1.df$N*bin1.df$sample
library(ggplot2)
c <- ggplot(data=data.predictions2, aes(predictions.treatment, predictions.outcome))
c <- c + geom_point(data=bin1.df,aes(mid, bin.mean,size=N))
c <- c + stat_smooth(method = "lm")
c <- c + ylab("Residualized Democratic Advantage in Candidate Quality")
c <- c + xlab("Residualized, Lagged Democratic
 Advantage in Districting Process")
 c <- c + theme_bw() +theme(axis.title=element_text(size=14),axis.text=element_text(size=14))
#c
pdf(file="Figures/congress_qual_D_adv.pdf")
c
dev.off()

ggsave(file="Figures/congress_qual_D_adv.eps")
c
dev.off()

## graph for mean vote share
state_elections_congress$random<-rnorm(dim(state_elections_congress)[1])
width=.1
reg2.graph.outcome2<-(felm(mean_voteshare~random|STATEAB+election, data=state_elections_congress[!is.na(state_elections_congress$factor.L1),]))
reg2.graph.treatment2<-(felm(scale(factor.L1)~random|STATEAB+election, data=state_elections_congress[!is.na(state_elections_congress$mean_voteshare),]))
predictions.outcome2<-resid(reg2.graph.outcome2)
predictions.treatment2<-resid(reg2.graph.treatment2)
data.predictions2<-state_elections_congress[  !is.na(state_elections_congress$mean_voteshare)& !is.na(state_elections_congress$factor.L1),]
data.predictions2<-select(data.predictions2, STATEAB, election)
colnames( predictions.outcome2)<-"predictions.outcome"
colnames( predictions.treatment2)<-"predictions.treatment"
data.predictions2<-cbind( predictions.outcome2, predictions.treatment2)

data.predictions2<-as.data.frame(data.predictions2)
graph <- mutate(data.predictions2, bin=cut(predictions.treatment, breaks=seq(-2,2, width)))
mean_outcome<-summarise(graph, mean(predictions.outcome))
mean_outcome_raw<-summarise(graph, mean(predictions.outcome))

graph$predictions.outcome<-graph$predictions.outcome-mean(graph$predictions.outcome)
bin1.df <- data.frame(

bin.mean=tapply(graph$predictions.outcome,
                         graph$bin, mean, na.rm=TRUE),
                     mid=seq(-2+width/2 , 2-width/2 , width),
                     N=tapply(graph$predictions.outcome, graph$bin, length))
bin1.df$N_adjusted<-bin1.df$N*bin1.df$sample
library(ggplot2)
c <- ggplot(data=data.predictions2, aes(predictions.treatment, predictions.outcome))
c <- c + geom_point(data=bin1.df,aes(mid, bin.mean,size=N))
c <- c + stat_smooth(method = "lm")
c <- c + ylab("Residualized Democratic Mean Vote Share")
c <- c + xlab("Residualized, Lagged Democratic
 Advantage in Districting Process")
 c <- c + theme_bw() +theme(axis.title=element_text(size=14),axis.text=element_text(size=14))
#c
pdf(file="Figures/congress_meanvoteshare.pdf")
c
dev.off()

ggsave(file="Figures/congress_meanvoteshare.eps")
c
dev.off()